//
//  MainViewController.swift
//  CJPWB
//
//  Created by 陈锦彭 on 16/3/12.
//  Copyright © 2016年 chenjinpeng. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    // MARK: - 属性
    //选中图片
    lazy var images : [String] = ["tabbar_home_highlighted", "tabbar_message_center_highlighted", "", "tabbar_discover_highlighted", "tabbar_profile_highlighted"]
    //中间发布按钮
    lazy var middleBtn : UIButton =  UIButton(imageName: "tabbar_compose_button", bgImageName: "tabbar_compose_icon_add")
    override func viewDidLoad() {
        super.viewDidLoad()
    
        setUpMiddleBtn()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //调整TabBarItems
        adjustTabBarItems()
        
    }
  

}


// MARK: - 调整tabbar相关
extension MainViewController {
    
    private func adjustTabBarItems() -> Void {
        for i in 0..<tabBar.items!.count{
           let item = tabBar.items![i]
            if i == 2 {
            
            item.enabled = false
                continue
            }
            item.selectedImage = UIImage(named:images[i])
        }
    
    }
    
    ///设置按钮的属性
    private func setUpMiddleBtn() -> Void {
        middleBtn.center = CGPoint(x: tabBar.bounds.width * 0.5, y: tabBar.bounds.height * 0.5)
        middleBtn.addTarget(self, action: "middleBtnDidClick", forControlEvents: .TouchUpInside)
        tabBar.addSubview(middleBtn)
    
    }

}


// MARK: - 点击事件处理
extension MainViewController {

    @objc private func middleBtnDidClick() {
     //弹出发布控制器
        let navVC = UINavigationController(rootViewController: ComposeViewController())
        presentViewController(navVC, animated: true, completion: nil)
    }
}

